package ddemo.viewAndcontroller;

import ddemo.pojo.*;
import ddemo.service.ParkinglotService;
import ddemo.service.ParkingspotService;

import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class parkView {
    public  static void park(User user){
        List<Parkinglot> parkinglots;
        parkinglots= ParkinglotService.searchParkinglots();
        System.out.println(parkinglots.size());
        Set<Integer> parkinglotids=new HashSet<>();
        while(true) {
            System.out.println("可用的停车站有:");
            for (Parkinglot c : parkinglots) {
                System.out.println("停车站id:" + c.getId() + " " + "停车站接口数:" + c.getParkNumber() + " " + "停车站位置:" + c.getPosition());
                parkinglotids.add(c.getId());
            }
            System.out.println("--------------------------------");
            System.out.println("请选择你要使用的停车站id:");
            int num;
            while (true) {
                try {
                    num = Integer.parseInt(new Scanner(System.in).next());
                } catch (NumberFormatException e) {
                    System.out.println("请输入正确的数字");
                    continue;
                }
                if (!parkinglotids.contains(num)) {
                    System.out.println("不存在对应的停车站,请重新输入");
                    continue;
                }
                break;
            }
            Set<Integer> parkingspotsids = new HashSet<>();
            List<Parkingspot> parkingspots = ParkingspotService.searchParkingspots(num);
            for (Parkingspot parks : parkingspots) {
                if ("可用".equals(parks.getUsestatus())) {
                    parkingspotsids.add(parks.getId());
                }
                System.out.println("停车位id：" + parks.getId() + " " + "停车位使用状态:" + parks.getUsestatus() + " " + "对应停车站id:" + parks.getId());
            }
            System.out.println("--------------------------------");

            System.out.println("请选择你要使用的停车位id:");
            while (true) {
                try {
                    num = Integer.parseInt(new Scanner(System.in).next());
                } catch (NumberFormatException e) {
                    System.out.println("请输入正确的数字");
                    continue;
                }
                break;
            }
            if (!parkingspotsids.contains(num)) {
                System.out.println("不存在对应的停车位或者停车位已被使用,请重新输入");
                continue;
            }

            for (Parkingspot  inter : parkingspots) {
                if (inter.getId() == num) {
                    inter.setUsestatus("已使用");
                    ParkinglotService.UpdateStatus("已使用", inter.getId(),user.getId());
                }
            }
            System.out.println("选择完成,您已可以开始使用停车位");
            System.out.println("--------------------------------");
            break;
        }
    }
}
